package com.m2bshop.shop.controller;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.m2bshop.shop.pojo.Order;
import com.m2bshop.shop.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/gysOrder")
public class OrderController {
    @Autowired
    private OrderService orderService;


    @GetMapping("/findAllBydzf")
    public String findAllBydzf(Model model,@RequestParam( name = "pageNum",defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        List<Order> orders=orderService.findAllBydzf();
        PageInfo<Order> pageInfo= new PageInfo<>(orders);
        model.addAttribute("pageInfo",pageInfo);
        return "orderdzf";
    }

    @GetMapping("/findAllBydfh")
    public String findAllBydfh(Model model,@RequestParam( name = "pageNum",defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        List<Order> orders=orderService.findAllBydfh();
        PageInfo<Order> pageInfo= new PageInfo<>(orders);
        model.addAttribute("pageInfo",pageInfo);
        return "orderdfh";
    }

    @GetMapping("/findAllByyfh")
    public String findAllByyfh(Model model,@RequestParam( name = "pageNum",defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        List<Order> orders=orderService.findAllByyfh();
        PageInfo<Order> pageInfo= new PageInfo<>(orders);
        model.addAttribute("pageInfo",pageInfo);
        return "orderyfh";
    }

    @GetMapping("/findAllByywc")
    public String findAllByywc(Model model,@RequestParam( name = "pageNum",defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        List<Order> orders=orderService.findAllByywc();
        PageInfo<Order> pageInfo= new PageInfo<>(orders);
        model.addAttribute("pageInfo",pageInfo);
        return "orderywc";
    }

    @GetMapping("/findAllByyqx")
    public String findAllByyqx(Model model,@RequestParam( name = "pageNum",defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        List<Order> orders=orderService.findAllByyqx();
        PageInfo<Order> pageInfo= new PageInfo<>(orders);
        model.addAttribute("pageInfo",pageInfo);
        return "orderyqx";
    }

    @RequestMapping(path= "/send/{id}")
    public String sendOndfh(@PathVariable("id") String id){
        orderService.sendOndfh(id);//修改状态
        return "redirect:/gysOrder/findAllBydfh";
    }

    @RequestMapping(path= "/track/{id}")
    public String addTracknumb(@PathVariable("id") String id,HttpServletRequest request){
        String tracknumb=request.getParameter("tracknumb");
        System.out.println(tracknumb);
        System.out.println(id);
        orderService.addTracknumb(id,tracknumb);//添加物流单号
        return "redirect:/gysOrder/findAllBydfh";
    }

    @RequestMapping(path= "/cancell/{id}")
    public String cancell(@PathVariable("id") String id){
        orderService.cancell(id);//修改状态
        return "redirect:/gysOrder/findAllByyfh";
    }

    @RequestMapping("/findBytitleOndzf")
    public String findBytitleOndzf(Model model,HttpServletRequest request
            ,@RequestParam( name = "pageNum",defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        String ctitle=request.getParameter("search");
        System.out.println(ctitle);
        List<Order> orders=orderService.findBytitle(ctitle);
        PageInfo<Order> pageInfo= new PageInfo<>(orders);
        model.addAttribute("pageInfo",pageInfo);
        return "orderdzf";
    };

    @RequestMapping("/findBytitleOndfh")
    public String findBytitleOndfh(Model model,HttpServletRequest request
            ,@RequestParam( name = "pageNum",defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        String ctitle=request.getParameter("search");
        System.out.println(ctitle);
        List<Order> orders=orderService.findBytitle(ctitle);
        PageInfo<Order> pageInfo= new PageInfo<>(orders);
        model.addAttribute("pageInfo",pageInfo);
        return "orderdfh";
    };

    @RequestMapping("/findBytitleOnyfh")
    public String findBytitleOnyfh(Model model,HttpServletRequest request
            ,@RequestParam( name = "pageNum",defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        String ctitle=request.getParameter("search");
        System.out.println(ctitle);
        List<Order> orders=orderService.findBytitle(ctitle);
        PageInfo<Order> pageInfo= new PageInfo<>(orders);
        model.addAttribute("pageInfo",pageInfo);
        return "orderyfh";
    };

    @RequestMapping("/findBytitleOnyqx")
    public String findBytitleOnyqx(Model model,HttpServletRequest request
            ,@RequestParam( name = "pageNum",defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        String ctitle=request.getParameter("search");
        System.out.println(ctitle);
        List<Order> orders=orderService.findBytitle(ctitle);
        PageInfo<Order> pageInfo= new PageInfo<>(orders);
        model.addAttribute("pageInfo",pageInfo);
        return "orderyqx";
    };

    @RequestMapping("/findBytitleOnywc")
    public String findBytitleOnywc(Model model,HttpServletRequest request
            ,@RequestParam( name = "pageNum",defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        String ctitle=request.getParameter("search");
        System.out.println(ctitle);
        List<Order> orders=orderService.findBytitle(ctitle);
        PageInfo<Order> pageInfo= new PageInfo<>(orders);
        model.addAttribute("pageInfo",pageInfo);
        return "orderywc";
    };

    @RequestMapping("/findBytitle")
    public String findBytitle(Model model,HttpServletRequest request
            ,@RequestParam( name = "pageNum",defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        String ctitle=request.getParameter("search");
        System.out.println(ctitle);
        List<Order> orders=orderService.findBytitle(ctitle);
        PageInfo<Order> pageInfo= new PageInfo<>(orders);
        model.addAttribute("pageInfo",pageInfo);
        return "findAll";
    };


    @RequestMapping("/findAll")
    public String findAll(Model model,@RequestParam( name = "pageNum",defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        System.out.println("开始查询");

        List<Order> orders=orderService.findAll();
        System.out.println(orders);

        PageInfo<Order> pageInfo= new PageInfo<>(orders);
        System.out.println(pageInfo);
        model.addAttribute("pageInfo",pageInfo);
        return "findAll";
    }

    @RequestMapping(path= "/goods")
    public String product(){
        return "product";
    }
}
